package com.llt.blog.controller;

import com.llt.blog.constants.Constant;
import com.llt.blog.model.BlogInfo;
import com.llt.blog.service.BolgService;
import com.llt.blog.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Resource
    BolgService bolgService;


    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList() {
        log.info("获取列表");

        return bolgService.queryBlogList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo queryBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("获取文章详情");
        BlogInfo blogInfo = bolgService.queryBlogDetail(blogId);

        // 获取登录的用户 判断登录用户是否是写博客的用户
        String token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if (userId != null && userId == blogInfo.getUserId()) {
            blogInfo.setIsLoginUser(true);
        }else {
            blogInfo.setIsLoginUser(false);
        }
        return blogInfo;
    }

    @RequestMapping("/add")
    public Boolean publishBlog(String title, String content, HttpServletRequest request) {
        log.info("开始发表文章");


        // 1. 参数校验
        if (!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return false;
        }

        // 2. 获取当前用户
        String token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if (userId == null || userId <= 0) {
            return false;
        }

        BlogInfo blogInfo = new BlogInfo(title, content, userId);
        Integer result = bolgService.publishBlog(blogInfo);

        if (result < 1) {
            log.error("发布失败");
            return false;
        }
        return true;
    }

    /*
     * 编辑博客
     * @date 2024/3/10 17:41
     */
    @RequestMapping("/update")
    public Boolean update(Integer blogId, String title, String content) {

        log.info("更新博客, 博客id: " + blogId);
        // 1. 参数校验
        if (blogId == null || !StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            log.error("id/标题/内容为空");
            return false;
        }

        BlogInfo blogInfo = new BlogInfo(blogId, title, content);

        bolgService.updateBlog(blogInfo);

        return true;
    }


    /*
     * 删除博客
     * @date 2024/3/10 17:41
     */
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId) {

        log.info("删除博客, blogId: " + blogId);

        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        bolgService.updateBlog(blogInfo);

        return true;
    }


}
